<?php

// 查立得PHP+mysql通用预约排队系统 V2024.12.12
// 演示地址: http://duilie.chalide.cn
// 文件路径: install.php
// 文件大小: 7206 字节
// 最后修改时间: 2024-12-17 21:00:00
// 作者: yujianyue
// 邮件: 15058593138@qq.com
// 版权所有,保留发行权和署名权
define('IN_SYSTEM', true);
require_once 'inc/conn.php';
require_once 'inc/pubs.php';

// 处理AJAX请求
if(isset($_GET['act'])) {
    switch($_GET['act']) {
        case 'install':
            doInstall();
            break;
        case 'demo':
            importDemo();
            break;
        default:
            json_msg(0, '未知操作');
    }
    exit;
}

// 创建数据库表
function doInstall() {
    global $conn;
    
    // 管理员表
    $sql_admin = "CREATE TABLE IF NOT EXISTS `admin` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `username` varchar(32) NOT NULL,
        `password` varchar(32) NOT NULL,
        `last_login` datetime DEFAULT NULL,
        `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (`id`),
        UNIQUE KEY `username` (`username`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
    
    // 医生表
    $sql_doctor = "CREATE TABLE IF NOT EXISTS `doctor` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `username` varchar(32) NOT NULL,
        `password` varchar(32) NOT NULL,
        `name` varchar(32) NOT NULL,
        `department` varchar(64) NOT NULL,
        `status` tinyint(1) DEFAULT '0' COMMENT '0关闭1开启',
        `end_time` datetime DEFAULT NULL COMMENT '结束时间',
        `last_login` datetime DEFAULT NULL,
        `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (`id`),
        UNIQUE KEY `username` (`username`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
    
    // 预约记录表
    $sql_booking = "CREATE TABLE IF NOT EXISTS `booking` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `doctor_id` int(11) NOT NULL,
        `department` varchar(64) NOT NULL,
        `doctor_name` varchar(32) NOT NULL,
        `booking_date` date NOT NULL,
        `booking_time` time NOT NULL,
        `patient_name` varchar(32) NOT NULL,
        `mobile` varchar(11) NOT NULL,
        `id_card` varchar(18) DEFAULT NULL,
        `sequence` varchar(4) NOT NULL COMMENT '顺序号',
        `verify_code` varchar(6) NOT NULL COMMENT '核销码',
        `status` tinyint(1) DEFAULT '0' COMMENT '0未核销1已核销',
        `verify_time` datetime DEFAULT NULL COMMENT '核销时间',
        `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (`id`),
        KEY `doctor_id` (`doctor_id`),
        KEY `booking_date` (`booking_date`),
        KEY `verify_code` (`verify_code`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
    
    try {
        // 执行建表SQL
        $conn->query($sql_admin);
        $conn->query($sql_doctor);
        $conn->query($sql_booking);
        
        // 创建默认管理员账号
        $admin_pwd = md5('admin888');
        $sql = "INSERT IGNORE INTO admin (username, password) VALUES ('admin', '$admin_pwd')";
        $conn->query($sql);
        
        json_msg(1, '数据库表创建成功');
    } catch(Exception $e) {
        json_msg(0, '数据库错误: ' . $e->getMessage());
    }
}

// 导入演示数据
function importDemo() {
    global $conn;
    
    try {
        // 导入30个医生账号
        $departments = ['内科', '外科', '儿科', '妇科', '眼科', '耳鼻喉科'];
        for($i = 1; $i <= 30; $i++) {
            $username = 'yiyiyi' . $i;
            $password = md5('123456');
            $name = '医生' . $i;
            $department = $departments[array_rand($departments)];
            
            $sql = "INSERT IGNORE INTO doctor (username, password, name, department) 
                   VALUES ('$username', '$password', '$name', '$department')";
            $conn->query($sql);
        }
        
        // 导入30条预约记录
        $names = ['张三', '李四', '王五', '赵六', '钱七', '孙八'];
        for($i = 1; $i <= 30; $i++) {
            $doctor_id = rand(1, 30);
            // 获取医生信息
            $sql = "SELECT name, department FROM doctor WHERE id = $doctor_id";
            $doctor = $conn->query($sql)->fetch_assoc();
            
            $booking_date = date('Y-m-d');
            $booking_time = date('H:i:s');
            $patient_name = $names[array_rand($names)];
            $mobile = '1' . rand(3,9) . rand(10000000,99999999);
            $id_card = '4' . rand(10000000000000000, 99999999999999999);
            $sequence = sprintf("%04d", $i);
            $verify_code = generate_verify_code();
            
            $sql = "INSERT INTO booking (doctor_id, department, doctor_name, booking_date, 
                   booking_time, patient_name, mobile, id_card, sequence, verify_code)
                   VALUES ($doctor_id, '{$doctor['department']}', '{$doctor['name']}', 
                   '$booking_date', '$booking_time', '$patient_name', '$mobile', 
                   '$id_card', '$sequence', '$verify_code')";
            $conn->query($sql);
        }
        
        json_msg(1, '演示数据导入成功');
    } catch(Exception $e) {
        json_msg(0, '数据库错误: ' . $e->getMessage());
    }
}

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>安装程序 - 预约排队系统</title>
    <style>
        body { font-family: Arial; max-width: 800px; margin: 0 auto; padding: 20px; }
        .btn { padding: 10px 20px; margin: 10px; cursor: pointer; }
        #msg { margin: 20px 0; padding: 10px; border-radius: 4px; }
        .success { background: #dff0d8; color: #3c763d; }
        .error { background: #f2dede; color: #a94442; }
    </style>
</head>
<body>
    <h1>预约排队系统安装程序</h1>
    <div>
        <button class="btn" onclick="install()">创建数据库表</button>
        <button class="btn" onclick="importDemo()">导入演示数据</button>
    </div>
    <div id="msg">用完请删本文件!</div>

    <script src="inc/js.js"></script>
    <script>
        function install() {
            ajax({
                url: 'install.php',
                type: 'GET',
                data: {act: 'install'},
                success: function(res) {
                    var data = JSON.parse(res);
                    showMessage(data.msg, data.code === 1);
                },
                error: function() {
                    showMessage('请求失败', false);
                }
            });
        }
        
        function importDemo() {
            ajax({
                url: 'install.php',
                type: 'GET',
                data: {act: 'demo'},
                success: function(res) {
                    var data = JSON.parse(res);
                    showMessage(data.msg, data.code === 1);
                },
                error: function() {
                    showMessage('请求失败', false);
                }
            });
        }
        
        function showMessage(msg, isSuccess) {
            var msgDiv = document.getElementById('msg');
            msgDiv.innerHTML = msg;
            msgDiv.className = isSuccess ? 'success' : 'error';
        }
    </script>
</body>
</html> 